﻿// 
//    Copyright 2010 The JaernCloud Project
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//        http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JaernCloud.Language.Compiler.Ast;
using System.IO;

namespace JaernCloud.Language.Compiler.Types {
    
    public class RootNodeType : INodeType {
        private List<INodeType> _children = new List<INodeType>();
        private AstNode _sourceNode;
        private List<INodeType> _inheritsFrom = new List<INodeType>(0);

        public RootNodeType(AstNode ast, bool loadDefaults) {
            this._sourceNode = ast;
            if (loadDefaults) {
                
                String html = "html { head { title: \"JaernCloud\" } }";
                
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(html))) {
                    
                    Scanner scanner = new Scanner(ms);
                    Parser parser = new Parser(scanner);
                    parser.Parse();

                    INodeType htmlNodeType = NodeTypeGenerator.GetNodeTypeInstanceFor(parser.Root.Children[0]);
                    this.AddChild(htmlNodeType);

                }
            }
        }

        public void AddChild(INodeType type) {
            type.Parent = this;
            _children.Add(type);

            // TODO: Remove html if another one is declared
        }


        public string Name {
            get { return "JaernCloud"; }
        }

        public List<string> Names {
            get { return new List<string>(new string[] { Name }); }
        }

        public List<INodeType> Children {
            get { return _children; }
        }

        public List<INodeType> InheritsFrom {
            get { return _inheritsFrom; }
        }

        public INodeType Parent {
            get {
                return null;
            }
            set {
                throw new InvalidOperationException();
            }
        }

        AstNode INodeType.SourceNode {
            get { return _sourceNode; }
        }

        public INodeType GetNodeTypeInstanceFor(AstNode node) {
            throw new NotImplementedException();
        }


        public bool Equals(INodeType other) {
            if (other is RootNodeType) {
                var mine = Children;
                var others = other.Children;
                if (mine.Count == others.Count) {
                    for (int i = 0; i < mine.Count; i++) {
                        if (!mine[i].Equals(others[i])) {
                            return false;
                        }
                    }
                    // no difference in children
                    return true;
                }
            }
            return false;
        }
    }
}
